<template>
  <div class="modal" @click="handleOverlayClick">
    <div class="modal-overlay"></div>
    <div class="modal-content">
      <div class="modal-header">
        <h3><i class="fas fa-user-circle"></i> 用户登录</h3>
        <button class="modal-close" @click="$emit('close')">
          <i class="fas fa-times"></i>
        </button>
      </div>
      <div class="modal-body">
        <form @submit="handleLogin">
          <div class="form-group">
            <label for="loginEmail">邮箱地址</label>
            <input 
              type="email" 
              id="loginEmail" 
              v-model="form.email"
              class="form-input" 
              placeholder="请输入邮箱地址" 
              required
            >
          </div>
          <div class="form-group">
            <label for="loginPassword">密码</label>
            <input 
              type="password" 
              id="loginPassword" 
              v-model="form.password"
              class="form-input" 
              placeholder="请输入密码" 
              required
            >
          </div>
          <div class="form-group">
            <label class="checkbox-label">
              <input type="checkbox" v-model="form.rememberMe">
              <span class="checkmark"></span>
              记住我
            </label>
          </div>
          <div class="form-actions">
            <button type="button" class="btn btn-ghost" @click="$emit('close')">
              取消
            </button>
            <button type="submit" class="btn btn-primary">
              登录
            </button>
          </div>
          <div class="form-footer">
            <p>还没有账户？<a href="#" @click="switchToRegister">立即注册</a></p>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
import apiService from '@/services/api.js'

export default {
  name: 'LoginModal',
  emits: ['close', 'login-success', 'switch-to-register'],
  data() {
    return {
      form: {
        email: '',
        password: '',
        rememberMe: false
      }
    }
  },
  methods: {
    async handleLogin(e) {
      e.preventDefault()
      
      try {
        // 使用API服务进行登录
        const response = await apiService.login(this.form.email, this.form.password)
        this.$emit('login-success', response.user)
        this.showAlert('登录成功！', 'success')
      } catch (error) {
        console.error('登录失败:', error)
        this.showAlert(error.message || '邮箱或密码错误', 'error')
      }
    },
    quickUniversalLogin() {
      // Method removed
    },
    switchToRegister() {
      this.$emit('close')
      this.$emit('switch-to-register')
    },
    handleOverlayClick(e) {
      if (e.target.classList.contains('modal') || e.target.classList.contains('modal-overlay')) {
        this.$emit('close')
      }
    },
    showAlert(message, type) {
      // Simple alert implementation - could be enhanced with a proper notification system
      const alertClass = type === 'success' ? 'alert-success' : 'alert-error'
      const alertDiv = document.createElement('div')
      alertDiv.className = `alert ${alertClass}`
      alertDiv.textContent = message
      alertDiv.style.cssText = `
        position: fixed;
        top: 20px;
        right: 20px;
        padding: 1rem 1.5rem;
        border-radius: 6px;
        color: white;
        font-size: 12px;
        z-index: 10000;
        background: ${type === 'success' ? '#10b981' : '#ef4444'};
      `
      
      document.body.appendChild(alertDiv)
      
      setTimeout(() => {
        if (alertDiv.parentNode) {
          alertDiv.parentNode.removeChild(alertDiv)
        }
      }, 3000)
    }
  }
}
</script>

<style scoped>
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  background: white;
  border-radius: 12px;
  max-width: 400px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5rem;
  border-bottom: 1px solid #e2e8f0;
}

.modal-header h3 {
  font-size: 14px;
  font-weight: 600;
  color: #1e293b;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.modal-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: #f1f5f9;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #64748b;
}

.modal-close:hover {
  background: #e2e8f0;
  color: #1e293b;
}

.modal-body {
  padding: 1.5rem;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-size: 12px;
  font-weight: 500;
  color: #374151;
}

.form-input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 12px;
  transition: border-color 0.2s ease;
}

.form-input:focus {
  outline: none;
  border-color: #3b82f6;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  font-size: 12px;
  color: #64748b;
}

.checkbox-label input[type="checkbox"] {
  width: auto;
}

.form-actions {
  display: flex;
  gap: 1rem;
  margin-top: 1.5rem;
}

.btn {
  flex: 1;
  padding: 0.75rem 1rem;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid transparent;
}

.btn-ghost {
  background: transparent;
  color: #64748b;
  border-color: #e2e8f0;
}

.btn-ghost:hover {
  background: #f8fafc;
  color: #1e293b;
}

.btn-primary {
  background: #3b82f6;
  color: white;
  border-color: #3b82f6;
}

.btn-primary:hover {
  background: #2563eb;
  border-color: #2563eb;
}

.form-footer {
  margin-top: 1.5rem;
  text-align: center;
}

.form-footer p {
  margin-bottom: 1rem;
  font-size: 12px;
  color: #64748b;
}

.form-footer a {
  color: #3b82f6;
  text-decoration: none;
}

.form-footer a:hover {
  text-decoration: underline;
}
</style>